package com.briup;

/*
 * @Description: 
 * @Author: Wen
 * @Date: 2024-11-22 09:03:39
 * @LastEditors: Wen
 * @LastEditTime: 2024-11-22 10:37:05
 * 太棒了，又坚持了一天
 */

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class Test {
    public static void main(String[] args)  {
        System.out.println("hello,test");
        Test test=new Test();
        String filePath="src/main/java/com/briup/class.xml";
        test.domParse(filePath);
    }
    //使用dom解析的文件 解析xml文件
    public void domParse(String filePath){
        // 1.创建解析类的对象
        DocumentBuilderFactory builderFactory= DocumentBuilderFactory.newInstance();
        try {
            // 2.通过工厂创建解析器对象
            DocumentBuilder documentBuilder=builderFactory.newDocumentBuilder();
            // 3.解析xml文件
            Document document = documentBuilder.parse(filePath);
            System.out.println("dom解析xml文件");
            // 4.获取根元素
            Element root = document.getDocumentElement();
            System.out.println("root:"+root);
            //5.获取root子节点
            NodeList childNodes = root.getChildNodes();
            //遍历子节点列表
            for(int i = 0;i < childNodes.getLength();i++) {
                Node node = childNodes.item(i);
                // System.out.println(node);
                //判断子节点的类型
                // System.out.println(node.getNodeType());
                // System.out.println(node.getNodeName());
                //1是元素 3是文本
                //只获取元素节点
                if (node.getNodeType() == 1) {
                    //  System.out.println(node.getNodeType());
                    //  System.out.println(node.getNodeName());
                    System.out.println(node.getNodeType());
                    NamedNodeMap attributes = node.getAttributes();
                    for (int j = 0; j < attributes.getLength(); j++) {
                        Attr attr = (Attr) attributes.item(j);
                        String nodeName = attr.getNodeName();
                        String attValue = attr.getValue();
                        System.out.println("nodeName" + nodeName);
                        System.out.println("attValue" + attValue);

                    }
                    // 获取当前节点的子节点
                    NodeList children = node.getChildNodes();
                    for (int k = 0; k < children.getLength(); k++) {
                        Node item = children.item(k);
                        //判断子节点是不是元素
                        if (item.getNodeType() == Node.ELEMENT_NODE) {
                            //节点名
                            String nodeName = item.getNodeName();
                            String nodeText = item.getTextContent();
                            System.out.println("nodeName=" + nodeName);
                            System.out.println("nodeText=" + nodeText);
                        }
                    }
                }
            }

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
